package com.hjqjl.common_lib.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.hjqjl.common_lib.utils.ViewBindingCreator;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

//AppCompatActivity换成RxAppCompatActivity是因为联网部分使用.compose(this.bindToLifecycle())防止内存泄漏
public class ComLibActivity<VB extends ViewBinding> extends RxAppCompatActivity {
    public Context pContext;
    public Activity pActivity;
    public VB pVBinding;
    public String pSimpleName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pContext = this;
        pActivity = this;
        ActivityCollector.getInstance().addActivity(this);
        pSimpleName = this.getClass().getSimpleName();
        LogUtils.v("位于的activity界面：" + pSimpleName);
        // 接收传递过来的参数，调用 inject 方法，这样使用 @Autowired 的会自动解析
        ARouter.getInstance().inject(this);

        //对ViewBinding的封装
        pVBinding = ViewBindingCreator.createViewBinding(getClass(), getLayoutInflater());
        View rootView = generateContentView(pVBinding == null ? getContentView() : pVBinding.getRoot());
        setContentView(rootView);

        initData();
        initListener();
        processLogic();
    }


    protected View getContentView() {
        return null;
    }

    //便于子类重载后扩充contentView,(这个意思，我这里没实现：例如BaseToolbarActivity就是实现了这个方法，对contentView进一步包装后再返回。)
    protected View generateContentView(View contentView) {
        return contentView;
    }

    /**
     * 初始化数据
     */
    public void initData() {
    }

    /**
     * 初始化点击事件
     */
    public void initListener() {
    }

    /**
     * 逻辑处理
     */
    public void processLogic() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.getInstance().removeActivity(this);
    }
}
